﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Composite.Presentation.Regions;
using System.Windows.Controls;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.Events;
using System.Windows.Forms.Integration;

namespace Infrastructure.WpfClient.Composite.Regions.SingleActiveRegion
{
    public class SingleActiveViewRegionAdapter : RegionAdapterBase<SingleActiveControl>
    {
        [Dependency]
        public IUnityContainer Container { get; set; }
        public SingleActiveViewRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory) : base(regionBehaviorFactory)
        {
        
        }
        
        protected override Microsoft.Practices.Composite.Regions.IRegion CreateRegion()
        {
            return Container.Resolve<SingleActiveViewRegion>();
        }
        protected override void Adapt(Microsoft.Practices.Composite.Regions.IRegion region, SingleActiveControl regionTarget)
        {
            bool contentIsSet = regionTarget.Content != null;
            region.ActiveViews.CollectionChanged += (sender, e) =>
            {
                switch (e.Action)
                {
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                        var view = region.ActiveViews.FirstOrDefault();
                        if (view != null)
                        {
                            regionTarget.Visibility = System.Windows.Visibility.Visible;
                            if (view is System.Windows.Forms.UserControl)
                            {
                                var host = new WindowsFormsHost();
                                host.Child = view as System.Windows.Forms.UserControl;
                                regionTarget.Content = host;
                            }
                            else
                            {
                                regionTarget.Content = view;
                            }                            
                        }
                            
                        break;
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
                        break;
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                        regionTarget.Content = null;
                        regionTarget.Visibility = System.Windows.Visibility.Collapsed;
                        break;
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
                        break;
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
                        break;
                    default:
                        break;
                }
                
            };
            
            region.Views.CollectionChanged += (sender , e)=>
            {
                switch (e.Action)
                {
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                        region.Activate(e.NewItems[0]);
                    break;                                        
                }
            };
        }
    }
}
